package com.ce.pms.customer.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.customer.dao.IParticipantRelationDao;
import com.ce.pms.customer.model.IndividualDO;
import com.ce.pms.customer.model.ParticipantDO;
import com.ce.pms.customer.model.ParticipantRelationDO;
import com.ce.util.BlankUtil;

/**
 * 功能描述:
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 付湘辉
 * @Created on 2008-05-26
 */
public class ParticipantRelationDaoImpl extends
		PmsBaseDaoImpl<ParticipantRelationDO> implements
		IParticipantRelationDao {

	/**
	 * 方法描述: 根据参与人ID查询与之相关的法人或者代理人<br>
	 * 此方法返回个人DO实体对象，可能是企业法人或代理人，类型信息由调用者区别
	 * @author 付湘辉
	 * @Created on 2008-05-26
	 *
	 * @param participantId
	 *            参与人ID
	 * @param relationTypeCode
	 *            参与人关系类型编码
	 * @return IndividualDO 个人DO实体对象
	 */
	public IndividualDO getOrganizationParticipant(Long participantId,
			String relationTypeCode) {

		if (BlankUtil.isBlank(participantId)) {
			return null;
		}

		ParticipantDO participantDO = null;
		StringBuilder hql = new StringBuilder("FROM ParticipantRelationDO AS pr ");
		hql.append("WHERE (pr.participant1.participantId=? OR pr.participant2.participantId=?) ");
		hql.append("AND pr.relationType.relationTypeCode=? ");

		Query query = super.createQuery(hql.toString(), participantId,participantId, relationTypeCode);
		ParticipantRelationDO participantRelationDO = null;
		List list = query.list();
		if (list.size()>0) {
			participantRelationDO = (ParticipantRelationDO)list.get(0);
		}else{
			return null;
		}

		IndividualDO individualDO = null;

		participantDO = participantRelationDO.getParticipant1();

		if (participantId.equals(participantDO.getParticipantId())) {
			individualDO = (IndividualDO)participantRelationDO.getParticipant2();
		} else {
			individualDO = (IndividualDO)participantDO;
		}

		return individualDO;
	}


	/**
	 * Function:根据参与人和参与人类型code获得参与人关系
	 * @param
	 * @return ParticipantRelationDO，如果没有查到结果 返回null
	 * Create author:xuhaidui
	 * Create on:2008-6-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */

	public ParticipantRelationDO queryParticipantRelation(Long participantId1, Long participantId2, Long relationTypeId) {
		ParticipantRelationDO participantRelationDO = null;
		StringBuffer queryStr = new StringBuffer();
		queryStr.append(" SELECT participantRelation FROM ParticipantRelationDO participantRelation ");
		queryStr.append(" WHERE (participantRelation.participant1.participantId=? AND participantRelation.participant2.participantId=?");
		queryStr.append(" OR participantRelation.participant1.participantId=? AND participantRelation.participant2.participantId=?) ");
		queryStr.append(" AND participantRelation.relationType.relationTypeId= ? ");
        Query query = super.createQuery(queryStr.toString(), participantId1,participantId2,participantId2,participantId1,relationTypeId);
		List list = query.list();
		if(list.size()>0) {
			participantRelationDO = (ParticipantRelationDO)list.get(0);
		}
        return participantRelationDO;
	}
	/**
	 * Function:获得两个参与人之间的关系
	 * @param
	 * @return ParticipantRelationDO，如果没有查到结果 返回null
	 * Create author:胡煦
	 * Create on:2008-7-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public ParticipantRelationDO queryParticipantRelation(Long participantId1, Long participantId2) {
		ParticipantRelationDO participantRelationDO = null;
		StringBuffer queryStr = new StringBuffer();
		queryStr.append(" SELECT participantRelation FROM ParticipantRelationDO participantRelation ");
		queryStr.append(" WHERE participantRelation.participant1.participantId=? ");
		queryStr.append(" AND participantRelation.participant2.participantId=? ");
		queryStr.append(" AND participantRelation.relationType.relationTypeCode in ('001','002','003','004','005','008')");
        Query query = super.createQuery(queryStr.toString(), participantId1,participantId2);
		List list = query.list();
		if(list.size()>0) {
			participantRelationDO = (ParticipantRelationDO)list.get(0);
		}
        return participantRelationDO;
	}


}
